<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="handleQuery"
      @clear="clear"
    />
    <div class="doumee_table_header">
      <el-button
        v-if="meta.indexOf('MemberInOutLeave') > -1"
        :loading="leaveLoading"
        type="primary"
        @click="MemberInOutLeave"
      >离场</el-button>
      <el-button
        v-if="meta.indexOf('MemberInOutRecordExport') > -1"
        :loading="exportLoading"
        @click="MemberInOutRecordExport"
      >导出</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        fixed="left"
        align="center"
      />
      <el-table-column
        align="center"
        label="入场时间"
        prop="inTime"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="会员信息"
        prop="member"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="入场凭证"
        prop="inFlag"
        min-width="100"
      >
        <template v-slot="scope">
          <span v-if="scope.row.inFlag == '42'">门票</span>
          <span v-else-if="scope.row.inFlag == '32'">场地预定</span>
          <span v-else-if="scope.row.inFlag == '41'">套餐</span>
          <span v-else>门票</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="产品"
        prop="productName"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="订单编号"
        prop="orderId"
        min-width="210"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="入场方式" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.inType == '1'">二维码</span>
          <span v-if="scope.row.inType == '2'">人脸</span>
          <span v-if="scope.row.inType == '3'">会员卡</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="在场时间"
        prop="durationOfPresence"
        min-width="160"
      />
      <el-table-column
        align="center"
        label="离场时间"
        prop="outTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        label="核销人员"
        prop="editor"
        min-width="120"
      />
      <el-table-column
        label="操作"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('MemberMealsCancelUse') > -1 && scope.row.inFlag == '41' && scope.row.flagInfo"
              v-preventReClick
              type="text"
              @click="toCx(scope.row)"
            >撤销</el-button>
            <span v-else>-</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <Pagination
        :total="totalCount"
        :page="pagination.page"
        @current-change="handlePageChange"
        @SizeChange="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import {
  inOutRecord,
  InOutLeave,
  InOutRevoke,
  inOutRecordExport
} from '@/api'
import dayjs from 'dayjs'
import { queryFormConfig } from './config'
import Pagination from '@/components/paginations'
import QueryForm from '@/components/QueryForm'
export default {
  name: 'MemberInOutRecord',
  components: {
    QueryForm,
    Pagination
  },
  data() {
    return {
      meta: [],
      list: [],
      selectList: [],
      totalCount: 0,
      listLoading: false,
      exportLoading: false,
      leaveLoading: false,
      pagination: {
        page: 1,
        rows: 10
      },
      queryFormConfig,
      filters: {
        daterange: []
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.filters.daterange.push(dayjs(new Date()).format('YYYY-MM-DD'))
    this.filters.daterange.push(dayjs(new Date()).format('YYYY-MM-DD'))
    this.getList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    toCx(e) {
      const that = this
      this.$confirm('此操作将撤销该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          InOutRevoke({
            param: {
              mealsMemberId: e.flagInfo,
              businessId: e.id
            }
          })
            .then((res) => {
              if (res.errorCode === '000000') {
                that.$message.success('撤销成功')
                that.getList()
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList()
    },
    handlePageChange(value) {
      this.pagination.page = value
      this.getList()
    },
    handleSelectionChange(val) {
      this.selectList = val
    },
    handleQuery() {
      this.pagination.page = 1
      this.getList()
    },
    getList() {
      const { filters, pagination } = this
      if (filters.daterange.length > 0) {
        filters.starTime = filters.daterange[0]
        filters.endTime = filters.daterange[1]
      }
      this.listLoading = true
      inOutRecord({
        pagination,
        param: {
          ...filters,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    MemberInOutRecordExport() {
      const { filters } = this
      this.exportLoading = true
      inOutRecordExport({
        param: { ...filters }
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.$message.success('导出成功')
        const a = document.createElement('a') // 创建一个a标签元素
        a.style.display = 'none' // 设置元素不可见
        a.href = res.record.showUrl // 设置下载地址
        document.body.appendChild(a) // 加入
        a.click() // 触发点击,下载
        document.body.removeChild(a)
        // window.open(res.record.showUrl, '_blank')
        this.exportLoading = false
      }, () => {
        this.exportLoading = false
      })
    },
    MemberInOutLeave() {
      if (this.selectList.length === 0) {
        return this.$message.warning('请先选择离场记录')
      }
      const ids = this.selectList.map(i => i.id)
      this.leaveLoading = true
      InOutLeave({
        param: {
          ids
        }
      }).then(res => {
        this.leaveLoading = false
        if (res.errorCode !== '000000') return
        this.$message.success('批量离场成功')
        this.getList()
      }, () => {
        this.leaveLoading = false
      })
    },
    clear() {
      this.filters = { daterange: [] }
      this.getList()
    }

  }
}
</script>
